//
//  LGDCCellContent.m
//  ALS3.0
//
//  Created by guomianxiang on 2017/4/7.
//  Copyright © 2017年 guomianxiang. All rights reserved.
//

#import "LGDCCellContent.h"
#import "LGDCTableHead.h"

#define k_name 0
#define k_sn 1
#define k_ip 2
#define k_port 3

#define k_section_controller 0
#define k_section_door 1


@implementation LGDCCellContent

- (instancetype)init {
    if (self = [super init]) {
        self.keyBoardType = UIReturnKeyDefault;
        self.cellClass = [LGShowCell class];
        self.select = YES;
        self.textFieldEnable = YES;
        self.sketchCellState = LGSketchCellDefaultState;
        self.cellTitleAlignment = LGDCCellTitleAlignmentRight;
        self.showRightArrow = NO;
    }
    return self;
}

- (instancetype)initLeftAlignmentAndshowRightArrow:(BOOL)show {
    if (self = [self init]) {
        self.cellTitleAlignment = LGDCCellTitleAlignmentLeft;
        self.showRightArrow = show;
    }
    return self;
}

- (void)setCellClass:(Class)cellClass {
    _cellClass = cellClass;
    
    LGHeadCell *head_Cell = [[LGHeadCell alloc]init];
    LGOpenDoorCell *openDoor_Cell = [[LGOpenDoorCell alloc]init];
    LGSketchCell *sketceh_Cell = [[LGSketchCell alloc]init];
    LGDislikeDoorCell *dislike_Cell = [[LGDislikeDoorCell alloc]init];
    if ([head_Cell isKindOfClass:cellClass]) {
        _cellHeight = k_Height_HeadCell;
    }else if ([openDoor_Cell isKindOfClass:cellClass]){
        _cellHeight = k_Height_OpenDoorCell;
    }else if ([sketceh_Cell isKindOfClass:cellClass] || [dislike_Cell isKindOfClass:cellClass]) {
        _cellHeight = k_Height_SketchCell;
    }else {
        _cellHeight = k_Height_DefaultCell;
    }
}
#pragma mark - seatchController
+ (LGDCCellContent *)transfromCellContentArrWithSearchResultController:(LGController *)resultController {
    LGDCCellContent *cellContent = [[LGDCCellContent alloc]init];
    
    cellContent.cellClass = [LGSketchCell class];
    cellContent.controller = resultController;
    return cellContent;
}

#pragma mark - showControllerTVC
+ (NSMutableArray<NSMutableArray<LGDCCellContent *> *> *)transfromCellContentArrWithControllersArr:(NSArray<NSArray<LGController *> *> *)controllersArr {
    
    NSMutableArray *cellContentArr = [NSMutableArray array];
    
    int row = 0;
    
    for (int i=0; i<controllersArr.count; i++) {
        NSMutableArray *temp = [NSMutableArray array];
        for (int j=0; j<controllersArr[i].count; j++) {
            
            
            LGDCCellContent *content = [[LGDCCellContent alloc]init];
            content.cellClass = [LGSketchCell class];
            content.controller = controllersArr[i][j];
            
            NSString *imageName = [NSString stringWithFormat:@"门禁%i",row % 3 + 1];
            
            content.image = [UIImage imageNamed:imageName];
            [temp addObject:content];
            
            row ++;
        }
        [cellContentArr addObject:temp];
    }
    
    return cellContentArr;
}
#pragma mark - controller info
+ (NSMutableArray<NSMutableArray<LGDCCellContent *> *> *)transfromShowControllerInfoCellContentArrWithController:(LGController *)showController {
    
    // 头
    NSMutableArray *arr = [self initControllerInfoBaseSectionWithController:showController];
    
    NSMutableArray *head = [NSMutableArray arrayWithObjects:arr[0], nil];
    [arr removeObjectAtIndex:0];
    
    // 控制器
    NSMutableArray *controllerInfo;
    if (showController.port.length == 0) {
        [arr removeLastObject];
    }
    controllerInfo = arr;
    
    // 门
    NSMutableArray *doorInfoArr = [NSMutableArray array];
    
    for (NSUInteger i=0; i<showController.doors.count; i++) {
        
        LGDoor *door = showController.doors[i];
        
        if (door.show == NO || door.valid == NO) {
            continue;
        }
        LGDCCellContent *doorContent = [[LGDCCellContent alloc]initLeftAlignmentAndshowRightArrow:NO];
        
        doorContent.cellClass = [LGShowCell class];
        doorContent.title = [NSString stringWithFormat:@"门%lu",i + 1];
//        doorContent.placehold = @"请输入门名称";
        doorContent.contentText = door.doorName;
        doorContent.select = door.show;
        [doorInfoArr addObject:doorContent];
    }
    
    return [NSMutableArray arrayWithObjects:head,controllerInfo,doorInfoArr, nil];
    
}

+ (NSMutableArray<NSMutableArray<LGDCCellContent *> *> *)transfromEditControllerInfoCellContentArrWithController:(LGController *)editController {
    
    NSMutableArray *cellContentArr = [NSMutableArray array];
    
    NSMutableArray<LGDCCellContent *> *controllerArr = [self initControllerInfoBaseSectionWithController:editController];
    controllerArr[k_sn].textFieldEnable = NO;
    
    [cellContentArr addObject:controllerArr];
    // 门信息－－－－－－－－－－－－－－－－－－－－
    NSMutableArray *doorInfoArr = [self initDoorInfoBaseSectionWithController:editController];
    [cellContentArr addObject:doorInfoArr];
    
    [self changeCellclass:cellContentArr toCellclass:[LGEditCell class]];
    
    return [self addButtonCell:cellContentArr withTitle:@"保存"];
}

+ (NSMutableArray<NSMutableArray<LGDCCellContent *> *> *)transfromAddControllerInfoCellContentArrWithController:(LGController *)addController {
    
    NSMutableArray *controllerInfoArr = [self initControllerInfoBaseSectionWithController:addController];
    
    NSMutableArray *doorInfoArr = [NSMutableArray array];
    
    NSInteger count = 1;
    
    if (addController.sn.length >= 4) {
        NSString *numberStr = [addController.sn substringWithRange:NSMakeRange(3, 1)];
        if ([numberStr integerValue]) {
            count = [numberStr integerValue];
        }
    }
    
    for (NSInteger i=0; i<count; i++) {
        LGDCCellContent *doorContent = [[LGDCCellContent alloc]initLeftAlignmentAndshowRightArrow:NO];
        doorContent.title = [NSString stringWithFormat:@"门%lu",i + 1];
        doorContent.placehold = @"请输入门名称";
        doorContent.select = YES;
        
        [doorInfoArr addObject:doorContent];
    }
    
    NSMutableArray *cellInfoArr = [NSMutableArray arrayWithObjects:controllerInfoArr,doorInfoArr, nil];
    
    [self changeCellclass:cellInfoArr toCellclass:[LGEditCell class]];
    
    return [self addButtonCell:cellInfoArr withTitle:@"添加"];
}

+ (LGController *)transfromLGControllerItemWithCellContentArr:(NSMutableArray<NSMutableArray<LGDCCellContent *> *> *)cellContentArr {
    
    NSMutableArray<LGDCCellContent *> *controllerInfoArr = cellContentArr[k_section_controller];
    
    LGController *controller = [[LGController alloc]init];
    controller.name = controllerInfoArr[k_name].contentText;
    controller.sn = controllerInfoArr[k_sn].contentText;
    controller.ip = controllerInfoArr[k_ip].contentText;
    controller.port = controllerInfoArr[k_port].contentText;
    
    NSMutableArray<LGDCCellContent *> *doorInfoArr = cellContentArr[k_section_door];
    NSMutableArray *doorsMarr = [NSMutableArray array];
    for (int i=0; i<doorInfoArr.count; i++) {
        LGDoor *door = [[LGDoor alloc]init];
        NSString *content = doorInfoArr[i].contentText;
        if (content.length == 0) {
            door.valid = NO;
            door.show = YES;
        }else {
            door.valid = YES;
            door.doorName = content;
            door.show = doorInfoArr[i].select;
        }
        
        door.doorIndex = i;
        //        NSLog(@"%@",door);
        [doorsMarr addObject:door];
    }
    
    controller.doors = [NSArray arrayWithArray:doorsMarr];
    
    return controller;
}

+ (NSMutableArray *)initControllerInfoBaseSectionWithController:(LGController *)controller {
    LGDCCellContent *item0 = [[LGDCCellContent alloc]init];
    item0.title = @"名称";
    item0.placehold = @"请输入控制器名称";
    item0.contentText = controller.name;
    item0.cellClass = [LGHeadCell class];
    
    LGDCCellContent *item1 = [[LGDCCellContent alloc]init];
    item1.title = @"S/N";
    item1.placehold = @"请输入S/N码";
    item1.contentText = controller.sn;
    item1.keyBoardType = UIKeyboardTypeASCIICapable;
    
    LGDCCellContent *item2 = [[LGDCCellContent alloc]init];
    item2.title = @"IP";
    item2.placehold = @"请输入IP地址";
    item2.contentText = controller.ip;
    item2.keyBoardType = UIKeyboardTypeASCIICapable;
    
    LGDCCellContent *item3 = [[LGDCCellContent alloc]init];
    item3.title = @"端口号";
    item3.placehold = @"请输入端口号(可不填)";
    item3.contentText = controller.port;
    item3.keyBoardType = UIKeyboardTypeNumbersAndPunctuation;
    
    NSMutableArray *controllerInfoArr = [NSMutableArray arrayWithObjects:item0, item1, item2, item3, nil];
    return controllerInfoArr;
}

+ (NSMutableArray *)initDoorInfoBaseSectionWithController:(LGController *)controller {
    NSMutableArray *doorInfoArr = [NSMutableArray array];
    
    for (NSUInteger i=0; i<controller.doors.count; i++) {
        
        LGDoor *door = controller.doors[i];
        
        LGDCCellContent *doorContent = [[LGDCCellContent alloc]initLeftAlignmentAndshowRightArrow:NO];

        doorContent.title = [NSString stringWithFormat:@"门%lu",i + 1];
        doorContent.placehold = @"请输入门名称";
        doorContent.contentText = door.doorName;
        doorContent.select = door.show;
        [doorInfoArr addObject:doorContent];
    }
    return doorInfoArr;
}

+ (void)changeCellclass:(NSArray *)arr toCellclass:(Class)class {
    for (id item in arr) {
        if ([item isKindOfClass:[NSArray class]]) {
            [self changeCellclass:item toCellclass:class];
        }
        if ([item isKindOfClass:[LGDCCellContent class]]) {
            LGDCCellContent *content = (LGDCCellContent *)item;
            content.cellClass = class;
        }
    }
}

+ (NSMutableArray *)addButtonCell:(NSMutableArray *)cellInfoArr withTitle:(NSString *)title{
    
    LGDCCellContent *buttonCellContent = [[LGDCCellContent alloc]init];
    buttonCellContent.title = title;
    buttonCellContent.cellClass = [LGButtonCell class];
    
    NSMutableArray *buttonCellArr = [NSMutableArray arrayWithObjects:buttonCellContent, nil];
    [cellInfoArr addObject:buttonCellArr];
    
    return cellInfoArr;
}


#pragma mark - showDoorTVC
// 返回导航数组
+ (NSArray *)getLetterOrderListSectionDataWith:(NSArray <NSArray <LGDoor *>*>*)orderListData {
    NSMutableArray *sectionData = [NSMutableArray array];
    
    [sectionData addObject:UITableViewIndexSearch];
    
    for (NSArray *item in orderListData) {
        LGDoor *door = item[0];
        char letter = [door.pinyin characterAtIndex:0];
        if (!isalpha(letter)) {
            letter = '#';
        }
        
        [sectionData addObject:[NSString stringWithFormat:@"%c",toupper(letter)]];
    }
    
    return sectionData;
}
//
+ (NSArray<NSArray<LGDCCellContent *> *> *)transfromCellContentArrWithLetterOrderDoorsArr:(NSArray<NSArray<LGDoor *> *> *)letterOrderDoorsArr {

    NSMutableArray *cellContentArr = [NSMutableArray array];
    
//    NSMutableArray *section0 = [NSMutableArray array];
//    LGDCCellContent *searchContent = [[LGDCCellContent alloc]init];
//    searchContent.cellClass = [LGSearchCell class];
//    searchContent.cellHeight = k_Height_DefaultCell;
//
//    [section0 addObject:searchContent];
//    [cellContentArr addObject:section0];
    
    
    int row = 0;

    for (int i=0; i<letterOrderDoorsArr.count; i++) {
        NSMutableArray *temp = [NSMutableArray array];
        for (int j=0; j<letterOrderDoorsArr[i].count; j++) {
            LGDCCellContent *content = [[LGDCCellContent alloc]init];
            content.cellClass = [LGSketchCell class];
            content.door = letterOrderDoorsArr[i][j];
            content.image = [UIImage imageNamed:[NSString stringWithFormat:@"门%i",row % 3 + 1]];
            content.cellHeight = k_Height_SketchCell;
            [temp addObject:content];
            
            row ++;
        }
        [cellContentArr addObject:temp];
    }
    
    return cellContentArr;
}

+ (NSArray<NSArray<LGDCCellContent *> *> *)transfromCellContentArrWithFrequencyDoorsArr:(NSArray <LGDoor *>*)frequencyDoorsArr {
    
    NSMutableArray <LGDCCellContent *>*temp = [NSMutableArray array];
    NSMutableArray <NSArray<LGDCCellContent *> *>*cellContentArr = [NSMutableArray arrayWithObjects:temp, nil];
    
    int row = 0;
    
    for (NSUInteger i=0; i<frequencyDoorsArr.count; i++) {
        LGDoor *door = frequencyDoorsArr[i];
        LGDCCellContent *content = [[LGDCCellContent alloc]init];
        
        if (door.favorite == -1) {
            content.cellClass = [LGDislikeDoorCell class];
            content.contentText = [NSString stringWithFormat:@"共计%li扇门不常用",frequencyDoorsArr.count - i];
            [temp addObject:content];
            break;
        }

        content.cellClass = [LGSketchCell class];
        content.sketchCellState = LGSketchCellFavoriteState;
        content.door = door;
        content.image = [UIImage imageNamed:[NSString stringWithFormat:@"门%i",row % 3 + 1]];
        row++;
        [temp addObject:content];
    }
//    NSLog(@"%@",cellContentArr);
    
//    for (int i=0; i<cellContentArr[0].count; i++) {
//        NSLog(@"++%@",cellContentArr[0][i].cellClass);
//    }
    
    return cellContentArr;

//    for (NSUInteger i=0; i<frequencyDoorsArr.count; i++) {
//        NSArray <LGDoor *>*doors = frequencyDoorsArr[i];
//        NSLog(@"++++%@",doors[0]);
//        if (doors[0].favorite == -1) {
//            
//            LGDCCellContent *content = [[LGDCCellContent alloc]init];
//            content.cellClass = [LGDislikeDoorCell class];
//            content.contentText = [NSString stringWithFormat:@"%li扇门不常用",doors.count];
//            [temp addObject:content];
//            continue;
//        }
//        NSLog(@"---------------------------");
//        for (NSUInteger j=0; j<doors.count; j++) {
//            LGDoor *door = doors[j];
//            NSLog(@"%@--%li",door.doorName,door.favorite);
//            LGDCCellContent *content = [[LGDCCellContent alloc]init];
//            content.cellClass = [LGSketchCell class];
//            if (door.favorite == 1) content.sketchCellState = LGSketchCellFavoriteState;
//            
//            NSLog(@"---- %li",content.sketchCellState);
//            
//            content.door = door;
//            content.image = [UIImage imageNamed:[NSString stringWithFormat:@"门%i",row % 3 + 1]];
//            [temp addObject:content];
//            row ++;
//            
//        }
//
//    }
//    
//    return cellContentArr;
}

+ (NSArray<NSArray<LGDCCellContent *> *> *)transfromCellContentArrWithDislikeDoorsArr:(NSArray <LGDoor *>*)dislikeDoorsArr {
    
    NSMutableArray <LGDCCellContent *>*temp = [NSMutableArray array];
    NSMutableArray *cellContentArr = [NSMutableArray arrayWithObjects:temp, nil];
    
    int row = 0;

    for (NSInteger i=0; i<dislikeDoorsArr.count; i++) {
        LGDoor *door = dislikeDoorsArr[i];
        if (door.favorite == -1) {
            LGDCCellContent *content = [[LGDCCellContent alloc]init];
            content.cellClass = [LGSketchCell class];
            content.image = [UIImage imageNamed:[NSString stringWithFormat:@"门%i",row % 3 + 1]];
            
            content.door = dislikeDoorsArr[i];
            
            row ++;
            [temp addObject:content];
        }
    }
    
    return cellContentArr;
}


+ (NSArray<NSArray<LGDCCellContent *> *> *)transfromSearchResultCellContentArrWithDoorsArr:(NSArray<NSArray<LGDoor *> *> *)doorsArr {
    NSMutableArray *cellContentArr = [NSMutableArray array];
    int row = 0;

    for (int i=0; i<doorsArr.count; i++) {
        NSMutableArray *temp = [NSMutableArray array];
        for (int j=0; j<doorsArr[i].count; j++) {
            LGDCCellContent *content = [[LGDCCellContent alloc]init];
            content.cellClass = [LGSketchCell class];

            content.door = doorsArr[i][j];
            content.sketchCellState = LGSketchCellSearchState;
            content.image = [UIImage imageNamed:[NSString stringWithFormat:@"seatchDoor%i",row % 3 + 1]];
            content.cellHeight = 60;
            [temp addObject:content];
            
            row ++;
        }
        [cellContentArr addObject:temp];
    }
    return cellContentArr;
}
#pragma mark - door info

+ (NSArray<NSArray<LGDCCellContent *> *> *)transfromCellContentArrWithDoor:(LGDoor *)door {
    NSMutableArray *cellInfoArr = [NSMutableArray array];
    
    [cellInfoArr addObject:[self initDoorInfoArrWith:door]];

    [cellInfoArr addObject:[self initControllerInfoArrWith:door]];

    NSArray *arr = [self initRelateDoorInfoArrWith:door];
    
    if (arr.count != 0) [cellInfoArr addObject:arr];

    return cellInfoArr;
}

+ (NSArray *)initDoorInfoArrWith:(LGDoor *)door {
    NSMutableArray *doorInfoArr = [NSMutableArray array];
    
    LGDCCellContent *content0 = [[LGDCCellContent alloc]init];
    content0.cellClass = [LGShowCell class];
    content0.title = @"门名称";
    content0.contentText = door.doorName;

    LGDCCellContent *content2 = [[LGDCCellContent alloc]init];
    content2.cellClass = [LGOpenDoorCell class];
    content2.door = door;
    
    
    [doorInfoArr addObject:content0];
    //    [doorInfoArr addObject:content1];
    [doorInfoArr addObject:content2];
    
    return doorInfoArr;
}

+ (NSArray *)initControllerInfoArrWith:(LGDoor *)door {
    NSMutableArray *controllerInfoArr = [NSMutableArray array];
    
    NSArray *controllers = DCMI.controllers;
    LGController *controller = controllers[door.controllerIndex];
    
    LGDCCellContent *content0 = [[LGDCCellContent alloc]init];
    content0.cellClass = [LGShowCell class];
    content0.title = @"名称";
    content0.contentText = controller.name;
    
    LGDCCellContent *content1 = [[LGDCCellContent alloc]init];
    content1.cellClass = [LGShowCell class];
    content1.title = @"S/N";
    content1.contentText = controller.sn;
    
    
    LGDCCellContent *content2 = [[LGDCCellContent alloc]init];
    content2.cellClass = [LGShowCell class];
    content2.title = @"IP";
    content2.contentText = controller.ip;
    
    
    [controllerInfoArr addObject:content0];
    [controllerInfoArr addObject:content1];
    [controllerInfoArr addObject:content2];
    
    if (controller.port.length != 0) {
        LGDCCellContent *content3 = [[LGDCCellContent alloc]init];
        content3.cellClass = [LGShowCell class];
        content3.title = @"端口号";
        content3.contentText = controller.port;
        [controllerInfoArr addObject:content3];
    }
    
    return controllerInfoArr;
}

+ (NSArray *)initRelateDoorInfoArrWith:(LGDoor *)door {
    NSMutableArray *relateDoorInfoArr = [NSMutableArray array];
    
    NSArray *controllers = [LGControllerManager shareControllerManager].controllers;
    LGController *controller = controllers[door.controllerIndex];
    NSArray *doors = controller.doors;

    for (int i=0; i<doors.count; i++) {
        LGDoor *item = doors[i];
        if (item.valid && item.show) {
            if ([item.doorName isEqualToString:door.doorName]) {
                continue;
            }
            LGDCCellContent *content = [[LGDCCellContent alloc]initLeftAlignmentAndshowRightArrow:NO];
            content.cellClass = [LGShowCell class];
            content.title = [NSString stringWithFormat:@"门%i",i];
            content.contentText = item.doorName;
            
            [relateDoorInfoArr addObject:content];
        }
    }
    
    if (relateDoorInfoArr.count == 0) {
        return nil;
    }

    return relateDoorInfoArr;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"%@",self.door];
}

@end
